# -*- coding:UTF-8 -*-#

'''
@author: qianwp
'''

from popcan.core.structures import UUIDModel,ModelType
from popcan.cache.cachebase import CacheObject,SetIndexObject
from popcan.redis.redisbase import RedisObject
from popcan.helper.datehelper import date2timef
from popcan.globals.settings import CustomerSettings,DesignerSettings,SessionSettings,AlbumSettings,CompositSettings,PhotoSettings,CommentSettings

class Customer(UUIDModel,CacheObject):
    
    def __init__(self,custId=0,email='',passwd='',nick='',regdate=None,logindate=None,upgraded = False):
        self.custId = custId
        self.email = email
        self.passwd = passwd
        self.nick = nick
        self.regdate = regdate
        self.logindate = logindate
        self.upgraded = upgraded #是否已经升级为设计师了
        self.ips = set()
    
    def mtype(self):
        return ModelType.Customer
    
    def key(self):
        return Customer.cachekey(self.custId)
    
    @staticmethod
    def cachekey(custId):
        return CustomerSettings.CUSTOMER_KEY_PREFIX % custId
    
    def read(self,buf):
        self.custId = buf.readLong()
        self.email = buf.readStr()
        self.passwd = buf.readStr()
        self.nick = buf.readStr()
        self.regdate = buf.readFloat()
        self.logindate = buf.readFloat()
        self.upgraded = buf.readBool()
        _size = buf.readShort()
        for _i in range(_size):
            self.ips.add(buf.readStr())
    
    def write(self,buf):
        buf.writeLong(self.custId)
        buf.writeStr(self.email)
        buf.writeStr(self.passwd)
        buf.writeStr(self.nick)
        buf.writeFloat(self.regdate)
        buf.writeFloat(self.logindate)
        buf.writeBool(self.upgraded)
        _size = len(self.ips)
        buf.writeShort(_size)
        for ip in self.ips:
            buf.writeStr(ip)
        
    def fromrow(self,row):
        self.custId = long(row[0])
        self.email = row[1]
        self.nick = row[2]
        self.passwd = row[3]
        self.regdate = date2timef(row[4])
        self.logindate = date2timef(row[5])
        self.upgraded = bool(int(row[6]))
    
class Designer(Customer):

    def __init__(self,custId=0,realname='',identnum='',contactnum='',address='',createdate=None):
        self.custId = custId
        self.realname = realname
        self.identnum = identnum
        self.contactnum = contactnum
        self.address = address
        self.createdate = createdate
    
    def key(self):
        return Designer.cachekey(self.custId)
    
    @staticmethod
    def cachekey(custId):
        return DesignerSettings.DESIGNER_KEY_PREFIX % custId
    
    def mtype(self):
        return ModelType.Designer
    
    def read(self,buf):
        self.custId = buf.readLong()
        self.realname = buf.readStr()
        self.identnum = buf.readStr()
        self.contactnum = buf.readStr()
        self.address = buf.readStr()
        self.createdate = buf.readFloat()
    
    def write(self,buf):
        buf.writeLong(self.custId)
        buf.writeStr(self.realname)
        buf.writeStr(self.identnum)
        buf.writeStr(self.contactnum)
        buf.writeStr(self.address)
        buf.writeFloat(self.createdate)
        
    def fromrow(self,row):
        self.custId = long(row[0])
        self.realname = row[1]
        self.identnum = row[2]
        self.contactnum = row[3]
        self.address = row[4]
        self.createdate = date2timef(row[5])

class AlbumIndex(SetIndexObject):
    
    def __init__(self,custid = 0):
        super(AlbumIndex,self).__init__()
        self.custId = custid
        
    def key(self):
        return AlbumIndex.cachekey(self.custId)
    
    @staticmethod
    def cachekey(key):
        return AlbumSettings.ALBUM_INDEX_KEY_PREFIX % key 

    def read(self,buf):
        self.custId = buf.readLong()
        super(AlbumIndex,self).read(buf)
    
    def write(self,buf):
        buf.writeLong(self.custId)
        super(AlbumIndex,self).write(buf)

class AlbumDesc(RedisObject):
    
    def __init__(self,albumId=0,desc=''):
        self.albumId = albumId
        self.desc = desc
        
    def read(self,buf):
        self.albumId = buf.readLong()
        self.desc = buf.readStr()
        
    def write(self,buf):
        buf.writeLong(self.albumId)
        buf.writeStr(self.desc)
        
    def key(self):
        return AlbumDesc.cachekey(self.albumId)
    
    @staticmethod
    def cachekey(key):
        return AlbumSettings.ALBUM_DESC_KEY_PREFIX % key

class Album(UUIDModel,CacheObject):
    
    def __init__(self,albumId=0,custid=0,name='',createdate=None):
        self.albumId = albumId
        self.custId = custid
        self.name = name
        self.createdate = createdate
        
    def read(self,buf):
        self.albumId = buf.readLong()
        self.custId = buf.readLong()
        self.name = buf.readStr()
        self.createdate = buf.readFloat()
    
    def write(self,buf):
        buf.writeLong(self.albumId)
        buf.writeLong(self.custId)
        buf.writeStr(self.name)
        buf.writeFloat(self.createdate)
    
    def fromrow(self,row):
        self.albumId = long(row[0])
        self.custId = long(row[1])
        self.name = str(row[2])
        self.createdate = date2timef(row[3])
    
    def key(self):
        return Album.cachekey(self.albumId)
    
    @staticmethod
    def cachekey(key):
        return AlbumSettings.ALBUM_KEY_PREFIX % key
    
    def mtype(self):
        return ModelType.Album

class CompositIndex(SetIndexObject):
    
    def __init__(self,albumId = 0):
        super(CompositIndex,self).__init__()
        self.albumId = albumId
        
    def key(self):
        return CompositIndex.cachekey(self.albumId)
    
    @staticmethod
    def cachekey(key):
        return CompositSettings.COMPOSIT_INDEX_KEY_PREFIX % key 

    def read(self,buf):
        self.compositId = buf.readLong()
        super(CompositIndex,self).read(buf)
    
    def write(self,buf):
        buf.writeLong(self.compositId)
        super(CompositIndex,self).write(buf)

class Composit(UUIDModel,CacheObject):
    
    def __init__(self,custId=0,albumId=0,compositId=0,name='',createdate=None):
        self.custId = custId
        self.albumId = albumId
        self.compositId = compositId
        self.name = name
        self.createdate = createdate
        
    def read(self,buf):
        self.custId = buf.readLong()
        self.albumId = buf.readLong()
        self.compositId = buf.readLong()
        self.name = buf.readStr()
        self.createdate = buf.readFloat()
        
    def write(self,buf):
        buf.writeLong(self.custId)
        buf.writeLong(self.albumId)
        buf.writeLong(self.compositId)
        buf.writeStr(self.name)
        buf.writeFloat(self.createdate)
        
    def fromrow(self,row):
        self.custId = long(row[0])
        self.albumId = long(row[1])
        self.compositId = long(row[2])
        self.name = str(row[3])
        self.createdate = date2timef(row[4])
        
    def key(self):
        return Composit.cachekey(self.albumId)
    
    @staticmethod
    def cachekey(key):
        return CompositSettings.COMPOSIT_KEY_PREFIX % key    
        
    def mtype(self):
        return ModelType.Composit
    
class PhotoIndex(SetIndexObject):
    
    def __init__(self,compositId=0):
        super(PhotoIndex,self).__init__()
        self.compositId = compositId
        
    def read(self,buf):
        self.compositId = buf.readLong()
        super(PhotoIndex,self).read(buf)

    def write(self,buf):
        buf.writeLong(self.compositId)
        super(PhotoIndex,self).write(buf)
    
class Photo(UUIDModel,CacheObject):
    
    def __init__(self,custId=0,albumId=0,compositId=0,photoId=0,url='',createdate=None):
        self.custId = custId
        self.albumId = albumId
        self.compositId = compositId
        self.photoId = photoId
        self.url = url
        self.createdate = createdate
        
    def read(self,buf):
        self.custId = buf.readLong()
        self.albumId = buf.readLong()
        self.compositId = buf.readLong()
        self.photoId = buf.readLong()
        self.url = buf.readStr()
        self.createdate = buf.readFloat()
    
    def write(self,buf):
        buf.writeLong(self.custId)
        buf.writeLong(self.albumId)
        buf.writeLong(self.compositId)
        buf.writeLong(self.photoId)
        buf.writeStr(self.url)
        buf.writeFloat(self.createdate)
    
    def fromrow(self,row):
        self.custId = long(row[0])
        self.albumId = long(row[1])
        self.compositId = long(row[2])
        self.photoId = long(row[3])
        self.url = str(row[4])
        self.createdate = date2timef(row[5])
        
    def key(self):
        return Photo.cachekey(self.albumId)
    
    @staticmethod
    def cachekey(key):
        return PhotoSettings.PHOTO_KEY_PREFIX % key    
    
    def mtype(self):
        return ModelType.Photo

class Comment(UUIDModel,CacheObject):
    
    def __init__(self,commentId=0,compositId=0,content='',fromcustId=0,createdate=None):
        self.commentId = commentId
        self.compositId = compositId
        self.content = content
        self.fromcustId = fromcustId
        self.createdate = createdate
    
    def read(self,buf):
        self.commentId = buf.readLong()
        self.compositId = buf.readLong()
        self.content = buf.readStr()
        self.fromcustId = buf.readLong()
        self.createdate = buf.readFloat()
    
    def write(self,buf):
        buf.writeLong(self.commentId)
        buf.writeLong(self.compositId)
        buf.writeLong(self.content)
        buf.writeLong(self.fromcustId)
        buf.writeFloat(self.createdate)
        
    def fromrow(self,row):
        self.commentId = long(row[0])
        self.compositId = long(row[1])
        self.content = str(row[2])
        self.fromcustId = long(row[3])
        self.createdate = date2timef(row[4])
    
    def key(self):
        return Comment.cachekey(self.albumId)
    
    @staticmethod
    def cachekey(key):
        return CommentSettings.COMMENT_KEY_PREFIX % key    
    
    def mtype(self):
        return ModelType.Comment

class Session(UUIDModel,CacheObject):
    
    def __init__(self,sessionId = '',custid='',remoteip=''):
        self.sessionId = sessionId
        self.custId = custid
        self.remoteip = remoteip
        
    def write(self,buf):
        buf.writeStr(self.sessionId)
        buf.writeLong(self.custId)
        buf.writeStr(self.remoteip)
        
    def read(self,buf):
        self.sessionId = buf.readStr()
        self.custId = buf.readLong()
        self.remoteip = buf.readStr()
        
    def mtype(self):
        return ModelType.Session
    
    def key(self):
        return Session.cachekey(self.sessionId)
    
    @staticmethod
    def cachekey(key):
        return SessionSettings.SESSION_KEY_PREFIX % key 
        